<?php
namespace logic\admin\index;
use library\common\collections\ArrayUtils;
use model\AdminMenu;
use model\AdminPower;
use think\Db;
/**
 * User: zj
 * Date: 2017/4/28
 * Time: 15:11
 * Progress: 95%
 */
class LAdminMenu
{

    //菜单树列表
    public function index_data(){
        $model = new AdminMenu();
//        $where['m.flag_use'] = 1;
        $where['m.flag_delete'] = 0;
        $list = $model->alias('m')
            ->join('admin_power p','p.id = m.power_id','left')
            ->field("m.*,m.parent_id as p_id,p.name as power_name")
            ->where($where)
            ->order('sort asc')
            ->select();
        $list_tree = $this->tree_asc($list);

        return $list_tree;
    }

    protected function tree_asc($list, $p_id = 0) {
        $list_tree = array();
        foreach ($list as $menu) {
            if ($menu['p_id'] == $p_id) {

                if (!in_array($menu, $list_tree)) {
                    array_push($list_tree, $menu);
                }
                $list_son = $this->tree_asc($list, $menu['id']);
                if ($list_son) {
                    foreach ($list_son as $menu_son) {
                        array_push($list_tree, $menu_son);
                    }
                }
            }
        }
        return $list_tree;
    }

    //菜单添加
    public function insert_data($param){

        $model = new AdminMenu();
        $res=$model->validate(true)->save($param);
        if($res){
            return array(
                'success'=>true,
                'message'=>'添加成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()
            );
        }
    }

    //编辑页面
    public function edit_data($id){
        $model=new AdminMenu();
        $model_power = new AdminPower();
        $data=$model->where(['id'=>$id])->find();
        $data=$data->getData();
        $result =$model->where(['id'=>$data['parent_id']])->field("name as parent_name")->find();

        if($result!=="" && $result!==null){
            $parent = $result->getData();
            $data['parent_name'] =  $parent['parent_name'];
        }else{
            $data['parent_name'] = null;
        }

        $result_power = $model_power->where(['id'=>$data['power_id']])->field("name as power_name,module,controller,function")->find();

        if($result_power!=="" && $result_power!==null){
            $power = $result_power->getData();
            $data['power_name'] = $power['power_name'].'('.$power['module'].'/'.$power['controller'].'/'.$power['function'].')';
        }else{
            $data['power_name'] =null;
        }

        return $data;
    }

    //菜单更新
    public function  update_data($id,$param){
        $model = new AdminMenu();
        $res = $model->validate("AdminMenu")->save($param,['id'=>$id]);
        if($res){
            return array(
                'success'=>true,
                'message'=>'修改成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()

            );
        }
    }

    //菜单删除
    public function delete_data($id){
        $model = new AdminMenu();

        $child = $model->where(['parent_id'=>$id])->find();

        if($child!==null){
            return array(
                'success'=>false,
                'message'=>'不能删除含有子菜单的选项',
            );

        }else{
            $res = $model->where(['id'=>$id])->delete();
            if($res){
                return array(
                    'success'=>true,
                    'message'=>'删除成功',
                );
            }else{
                return array(
                    'success'=>false,
                    'message'=>$model->getError()
                );
            }
        }

    }

    //获取权限名称
    public function power_data(){
        $model = new AdminPower();
        $where['flag_use'] = 1;
        $where['flag_delete'] = 0;
        $list = $model->where($where)->field("id,parent_id as p_id,name")->order('sort asc')->select();
        $list_tree = $this->tree_asc($list);

        return $list_tree;

    }

    public function treeview_data(){
        $model = new AdminMenu();
        $where['flag_use'] = 1;
        $where['flag_delete'] = 0;
        $result = $model->where($where)->field("id,parent_id,name as text")->order('sort asc')->select();
        $list = json_decode(json_encode($result),true);
        return ArrayUtils::arrayToTree($list,'id','parent_id','nodes','0')['nodes'];
    }

    //power_id更新
    public function  set_power($id,$param){
        $model = new AdminMenu();
        $res = $model->save($param,['id'=>$id]);
        if($res){
            return array(
                'success'=>true,
                'message'=>'修改成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()

            );
        }
    }


    public function power_treeview(){
        $model = new AdminPower();
        $where['flag_use'] = 1;
        $where['flag_delete'] = 0;
        $result = $model->where($where)->field("id,parent_id,name as text")->order('sort asc')->select();
        $list = json_decode(json_encode($result),true);
        return ArrayUtils::arrayToTree($list,'id','parent_id','nodes','0')['nodes'];
    }


}